package com.taobao.askwho.action;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.taobao.askwho.ao.ComplexAo;
import com.taobao.askwho.utils.data.WorkflowXmlUtil;
import com.taobao.askwho.vo.ResultPo;
@Controller                 
@RequestMapping("/personopt.do")
public class PersonAction extends BaseAction {
	@RequestMapping(params = "method=addNewPerson")
	public void addNewPerson(String personName, boolean isPublic, String tagString,String wangwang, String userUuid, 
			HttpServletResponse response){
		ResultPo po = new ResultPo();
		if(userUuid == null || userUuid.trim().equals("")) {
			po.setSuccess(false);
			po.setMsg("无用户信息");
			try {
				this.writeJSONToResponseJSONObject(response, po);
			} catch (IOException e) {
				logger.error("", e);
			}
			return;
		}
		po = addNewPerson(personName, true, tagString, wangwang, userUuid);
		try {
			this.writeJSONToResponseJSONObject(response, po);
		} catch (IOException e) {
			logger.error("", e);
		}
	}
	
	private ResultPo addNewPerson(String personName, boolean isPublic, String tagString,String wangwang, String userUuid){
		final ResultPo po = new ResultPo();
		if(userUuid == null || userUuid.trim().equals("")) {
			po.setSuccess(false);
			po.setMsg("无用户信息");
			return po;
		}
		
		if(tagString == null)
			tagString = "暂无标签";
		else {
			tagString = tagString.trim();
			tagString = tagString.replace('，', ',');
		}
		
		//String[] tagNameArray = tagString.trim().split(",");
		String[] tagNameArray = new String[]{tagString};
		try {
			ComplexAo.get().addNewPerson(personName, isPublic, wangwang, tagNameArray, userUuid);
			po.setSuccess(true);
		} catch (Exception e) {
			logger.error("", e);
			po.setSuccess(false);
			po.setMsg(e.getMessage());
		}
		return po;
	}	
	
	@RequestMapping(params = "method=addNewPersonBySystem")
	public void addNewPersonBySystem(HttpServletResponse response){
		Map<String, String> map = WorkflowXmlUtil.loadWorkFlowXml();
		for(Entry<String, String> entry :map.entrySet()) {
			addNewPerson(entry.getValue(), true, entry.getKey(), entry.getValue(), "person_uuid");
		}
		this.writeHTMLToResponse("ok", response);
	}	
}
